home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / comms / other / amarquee / examples / geturl.c < prev    next >
C/C++ Source or Header  |  1999-05-25  |  3KB  |  102 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13. /* #define ASYNC_CONNECT */
  14.  
  15. struct Library * AMarqueeBase = NULL;
  16. struct QSession * session     = NULL;
  17.  
  18. void CleanExit(void)
  19. {
  20.     printf("\nCleaning up...\n");
  21.     if (session)      QFreeSession(session);        /* This MUST be done before we close the library! */
  22.     if (AMarqueeBase) CloseLibrary(AMarqueeBase);  
  23.     printf("All done.\n");
  24. }
  25.  
  26. /* Main program */
  27. int main(int argc, char ** argv)
  28. {
  29.     char * connectTo;
  30.     int port;
  31.     char *connectStr="";
  32.     BOOL terminate=FALSE;
  33.      
  34.     printf("Usage Note:  GetURL [url=localhost] [port=80]\n");  
  35.     printf("e.g. GetURL \"ACS.hostile.cx\" port=80\n");
  36.  
  37.     atexit(CleanExit);
  38.   
  39.     connectTo = (argc>1) ? argv[1] : "localhost";
  40.     port      = (argc>2) ? atoi(argv[2]) : 80;
  41.  
  42.     if ((AMarqueeBase = OpenLibrary("amarquee.library",49L)) == NULL)
  43.     {
  44.         printf("Couldn't open amarquee.library v49!\n");
  45.         exit(RETURN_ERROR);
  46.     }
  47.     printf("Connecting to %s:%i\n",connectTo, port);
  48.  
  49.     #ifdef ASYNC_CONNECT
  50.     if ((session = QNewSocketSessionAsync(connectTo, port,NULL)) == NULL)
  51.     {
  52.         printf("Couldn't connect to server %s:%i\n",connectTo, port);
  53.         exit(RETURN_WARN);
  54.     }
  55.     #else
  56.     if ((session = QNewSocketSession(connectTo, port,NULL)) == NULL)
  57.     {
  58.         printf("Couldn't connect to server %s:%i\n",connectTo, port);
  59.         exit(RETURN_WARN);
  60.     }
  61.     #endif
  62.  
  63.     printf("Connected to server %s:%i\n",connectTo, port);
  64.     printf("Retrieving index.html from %s ...\n", connectTo);
  65.  
  66.     connectStr="GET / HTTP/1.0\n\n";
  67.     QSendRawOp(session,connectStr,strlen(connectStr));
  68.     QGo(session,0L); /* Send it! */
  69.  
  70.     /* Wait for messages */
  71.     while(!terminate)
  72.     {
  73.         struct QMessage * qMsg;
  74.         ULONG signals = (1L << session->qMsgPort->mp_SigBit) | (SIGBREAKF_CTRL_C);
  75.  
  76.         /* Wait for next message from the server */
  77.         signals = Wait(signals);
  78.      
  79.         if (signals & (1L << session->qMsgPort->mp_SigBit))
  80.         {
  81.             while(qMsg = (struct QMessage *) GetMsg(session->qMsgPort))
  82.             {
  83.                 if (qMsg->qm_Status!=QERROR_NO_ERROR) {
  84.                     // Got an error!
  85.                     if (qMsg->qm_Status==QERROR_NO_CONNECTION) {
  86.                         terminate=TRUE;
  87.                         printf("Connection to remote host was closed.\n");
  88.                     }
  89.                 }
  90.                  else if (qMsg->qm_ID==0 && qMsg->qm_DataLen>0) { // Data sent to us
  91.                     // Print it out
  92.                     printf("**** START PACKET ****\n%s\n**** END PACKET ****\n",qMsg->qm_Data);
  93.                 }
  94.  
  95.                 FreeQMessage(session, qMsg);
  96.             }
  97.         }
  98.         if (signals & SIGBREAKF_CTRL_C) break;  /* Quit if CTRL-C pressed */
  99.     }
  100.     /* CleanExit() called here! */
  101. }
  102.